import CryptoJS from './crypto'

// {"key_code":"f5d308da2b01002ab4429fefc5ee9648","version_code":"96","user_id":"100242875","sub_type":"5","uuid":"0fe2caecbfdf48b4"}

/**
 * 加密算法
 */
export function encodeString (params) {
  var user = JSON.parse(sessionStorage.getItem('user')) === null ? {} : JSON.parse(sessionStorage.getItem('user'))
  var user_id = user.user_id
  var version_code = user.version_code
  var subType = user.sub_type
  params = Object.assign({
    'user_id': user_id,
    'userId': user_id,
    'version_code': version_code,
    'versionCode': version_code,
    'sub_type': subType,
    'subType': subType
  }, params)
  var jsonParams = JSON.stringify(params)

  console.log(jsonParams)

  var keyutf8 = CryptoJS.enc.Utf8.parse(user.key_code)
  var encryptPram = {
    uuid: user.uuid || '653002a45f9d10be',
    data: CryptoJS.AES.encrypt(jsonParams, keyutf8, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.ZeroPadding
    }).toString()
  }
  // var encryt = JSON.stringify(encryptPram);
  var encryt = encryptPram

  return encryt // json2obj
}
/**
 * 解密算法
 */
export function decodeString (data) {
  var user = JSON.parse(sessionStorage.getItem('user')) === null ? {} : JSON.parse(sessionStorage.getItem('user'))
  var keyutf8 = CryptoJS.enc.Utf8.parse(user.key_code)
  var resp = CryptoJS.AES.decrypt(data, keyutf8, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.ZeroPadding
  }).toString(CryptoJS.enc.Utf8)

  var json = JSON.parse(resp.replace(/[\x00-\x1f]*$/g, '')) // eslint-disable-line

  if (json.error_code) {
    if (json.error_code) {
      alert(json.error)
    }
  } else {
    return json
  }
}

/**
 * 获取设备信息
 */
let mobilesSystemObj = null
function getMobilesSystemObj () {
  if (mobilesSystemObj === null) {
    let u = navigator.userAgent
    // let app = navigator.appVersion
    let nativeVersionsObj = {
      ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
      android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, // android终端或者uc浏览器
      iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, // 是否为iPhone或者QQHD浏览器
      iPad: u.indexOf('iPad') > -1 // 是否iPad
    }
    mobilesSystemObj = {
      isIos: nativeVersionsObj.ios || nativeVersionsObj.iPhone || nativeVersionsObj.iPad,
      isAndroid: nativeVersionsObj.android
    }
  }
  return mobilesSystemObj
}

export const isIos = getMobilesSystemObj().isIos
export const isAndroid = getMobilesSystemObj().isAndroid

/**
 * 时间转换器
 */
// 时间戳转换年月日
function timeToday (times) {
  if (times === '' || times == null) {
    return ''
  }
  return timesChange(times, 1)
  // var regTime1=new Date(times)
  // return regTime1.toLocaleDateString().replace(/\//g, "-");
}
// 时间戳转换年月日时分秒
function timeToSecond (times) {
  if (times === '') {
    return ''
  }
  return timesChange(times, 2)
  // var regTime1=new Date(times)
  // return regTime1.toLocaleDateString().replace(/\//g, "-") + " " + regTime1.toTimeString().substr(0, 8);
}
// 时间戳转换时分秒
function timeTOoOnlySecond (times) {
  if (times === '') {
    return ''
  }
  return timesChange(times, 3)
  // var regTime1=new Date(times)
  // return regTime1.toLocaleDateString().replace(/\//g, "-") + " " + regTime1.toTimeString().substr(0, 8);
}

// 时间转换基础
function timesChange (times, flag) {
  var date = new Date(times)
  var Y = date.getFullYear() + '-'
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' '
  var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':'
  var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':'
  var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds())
  if (flag === 1) {
    return Y + M + D
  } else if (flag === 2) {
    return Y + M + D + h + m + s
  } else {
    return h + m + s
  }
}

export const timeConverter = {
  day: timeToday,
  second: timeToSecond,
  onlySecond: timeTOoOnlySecond
}
